/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.poll.pollwqi.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.poll.common.constant.OmConstant;
import com.hopes.bizz.poll.pollwqi.entity.PollWqiDataEntity;
import com.hopes.bizz.poll.pollwqi.service.PollWqiDataService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 	WQI数据计算
 *
 * @author cx
 * @date 2023-10-16 13:03:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wqiData")
@Tag(description = "wqiData", name = "  WQI指数管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PollWqiDataController {

    private final PollWqiDataService pollWqiDataService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param pollWqiData WQI指数
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('poll_WqiData_view')")
    public R getpollWqiDataPage(Page page, PollWqiDataEntity pollWqiData) {
        LambdaQueryWrapper<PollWqiDataEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(pollWqiDataService.page(page, wrapper));
    }


    /**
     * 通过id查询WQI指数
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('poll_WqiData_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(pollWqiDataService.getById(id));
    }



    /**
     * 查询实时WQI指数
     *
     * @return R
     */
    @Operation(summary = "查询实时WQI指数", description = "查询实时WQI指数")
    @GetMapping("/rt/{monitorSiteId}")
//    @PreAuthorize("@pms.hasPermission('poll_WqiData_view')")
    public R getRtDate(@PathVariable("monitorSiteId") Long monitorSiteId) {
        pollWqiDataService.calculateWQIRtHandler(monitorSiteId);
        PollWqiDataEntity one = pollWqiDataService.lambdaQuery()
                .eq(PollWqiDataEntity::getDataType, OmConstant.DATA_TYPE_RT)
                .orderByDesc(PollWqiDataEntity::getDataTime)
                .last("limit 1").one();
        return R.ok(one);
    }
    /**
     * 查询分钟WQI指数
     *
     * @return R
     */
    @Operation(summary = "查询分钟WQI指数", description = "查询分钟WQI指数")
    @GetMapping("/min")
    @PreAuthorize("@pms.hasPermission('poll_WqiData_view')")
    public R getMinDate() {
        PollWqiDataEntity one = pollWqiDataService.lambdaQuery()
                .eq(PollWqiDataEntity::getDataType, OmConstant.DATA_TYPE_MIN)
                .orderByDesc(PollWqiDataEntity::getDataTime)
                .last("limit 1").one();
        return R.ok(one);
    }

    /**
     * 查询天WQI指数
     *
     * @return R
     */
    @Operation(summary = "查询天WQI指数", description = "查询天WQI指数")
    @GetMapping("/day")
    @PreAuthorize("@pms.hasPermission('poll_WqiData_view')")
    public R getDayDate() {
        JSONObject res = pollWqiDataService.getDayDate();

        return R.ok(res);
    }


    /**
     * 新增WQI指数
     *
     * @param pollWqiData WQI指数
     * @return R
     */
    @Operation(summary = "新增WQI指数", description = "新增WQI指数")
    @SysLog("新增WQI指数")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('poll_WqiData_add')")
    public R save(@RequestBody PollWqiDataEntity pollWqiData) {
        return R.ok(pollWqiDataService.save(pollWqiData));
    }

    /**
     * 修改WQI指数
     *
     * @param pollWqiData WQI指数
     * @return R
     */
    @Operation(summary = "修改WQI指数", description = "修改WQI指数")
    @SysLog("修改WQI指数")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('poll_WqiData_edit')")
    public R updateById(@RequestBody PollWqiDataEntity pollWqiData) {
        return R.ok(pollWqiDataService.updateById(pollWqiData));
    }

    /**
     * 通过id删除WQI指数
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除WQI指数", description = "通过id删除WQI指数")
    @SysLog("通过id删除WQI指数")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('poll_WqiData_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(pollWqiDataService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param pollWqiData 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('poll_WqiData_export')")
    public List<PollWqiDataEntity> export(PollWqiDataEntity pollWqiData) {
        return pollWqiDataService.list(Wrappers.query(pollWqiData));
    }


}
